package com.mtnz.njk.Service;

import cn.hutool.core.util.IdUtil;
import com.github.pagehelper.PageHelper;
import com.mtnz.njk.Mapper.MemberManagerMapper;
import com.mtnz.njk.Mapper.NjkUserServiceMapper;
import com.mtnz.njk.Mapper.ServiceAreaMapper;
import com.mtnz.njk.entity.NjkUserServiceEntity;
import com.mtnz.njk.entity.ServiceAreaEntity;
import com.mtnz.njk.result.PageInfo;
import com.mtnz.njk.result.ResponseMessage;
import com.mtnz.njk.result.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.List;

/**
 * @Description: 成员管理服务层
 * @Author: FBL
 * @CreateDate: 2019\8\26 0026 15:55
 * @Version: 1.0
 */
@Service
public class MemberManagerService {

    @Autowired
    MemberManagerMapper memberManagerMapper;

    @Autowired
    ServiceAreaMapper serviceAreaMapper;

    @Autowired
    NjkUserServiceMapper serviceMapper;

    /**
     * 成员管理列表
     *
     * @param key
     * @param pageNum
     * @param pageSize
     * @return
     * @throws Exception
     */
    public ResponseResult listMember(String key, Integer pageNum, Integer pageSize) throws Exception {
        PageHelper.startPage(pageNum, pageSize);
        // 查询成员列表
        List<NjkUserServiceEntity> njkUserServiceEntities = memberManagerMapper.listMember(key);
        PageInfo<NjkUserServiceEntity> njkUserServiceEntityPageInfo = new PageInfo<>(njkUserServiceEntities);
        List<NjkUserServiceEntity> list = njkUserServiceEntityPageInfo.getList();
        for (NjkUserServiceEntity njkUserServiceEntity : list) {
            // 查询成员负责区域
            Example serviceAreaExample = new Example(ServiceAreaEntity.class);
            serviceAreaExample.createCriteria().andEqualTo("serviceId", njkUserServiceEntity.getServiceId());
            List<ServiceAreaEntity> serviceAreaEntities = serviceAreaMapper.selectByExample(serviceAreaExample);
            njkUserServiceEntity.setServiceAreaEntities(serviceAreaEntities);
        }
        return new ResponseResult(ResponseMessage.OK, njkUserServiceEntityPageInfo);
    }

    /**
     * 成员管理列表编辑
     *
     * @param serviceEntity
     * @return
     * @throws Exception
     */
    @Transactional
    public ResponseResult updateMember(NjkUserServiceEntity serviceEntity) throws Exception {
        // 修改成员基本信息

        serviceMapper.updateByPrimaryKeySelective(serviceEntity);
        if (serviceEntity.getServiceAreaEntities() != null && serviceEntity.getServiceAreaEntities().size() > 0) {
            // 去除以前的区域信息
            if (serviceEntity.getOldAreaId() != null && serviceEntity.getOldAreaId().size() > 0) {
                List<Integer> oldAreaId = serviceEntity.getOldAreaId();
                Example serviceAreaExample = new Example(ServiceAreaEntity.class);
                serviceAreaExample.createCriteria().andIn("areaId", oldAreaId);
                serviceAreaMapper.deleteByExample(serviceAreaExample);
            }
            // 新增现在的区域信息
            List<ServiceAreaEntity> serviceAreaEntities = serviceEntity.getServiceAreaEntities();
            for (ServiceAreaEntity serviceAreaEntity : serviceAreaEntities) {
                serviceAreaEntity.setCreateTime(new Date());
                serviceAreaEntity.setUpdateTime(new Date());
                serviceAreaMapper.insertSelective(serviceAreaEntity);
            }
        }
        return new ResponseResult(ResponseMessage.OK);
    }

    /**
     * 成员管理列表新增
     *
     * @param serviceEntity
     * @return
     * @throws Exception
     */
    public ResponseResult insertMember(NjkUserServiceEntity serviceEntity) throws Exception {
        String id = IdUtil.simpleUUID();
        serviceEntity.setServiceId(id);
        // 添加成员基本信息
          int i = 0;
        i += serviceMapper.insertSelective(serviceEntity);
        if (serviceEntity.getServiceAreaEntities() != null && serviceEntity.getServiceAreaEntities().size() > 0) {
            // 添加成员负责区域
            List<ServiceAreaEntity> serviceAreaEntities = serviceEntity.getServiceAreaEntities();
            serviceAreaEntities.forEach(s -> {
                s.setCreateTime(new Date());
                s.setUpdateTime(new Date());
                s.setServiceId(serviceEntity.getServiceId());
                serviceAreaMapper.insertSelective(s);
            });
        }
        if (i > 0)
            return new ResponseResult(ResponseMessage.OK);
        return new ResponseResult(ResponseMessage.FAIL);
    }
}
